﻿using Novell.Directory.Ldap;
using Novell.Directory.Ldap.Controls;
using Novell.Directory.Ldap.Utilclass;
using System;
using System.Collections;
using System.Collections.Generic;
//using No

namespace LDAPApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            //*
            using (var cn = new LdapConnection())
            {
                // connect
                cn.Connect("192.168.99.2", 389);
                // bind with an username and password
                // this how you can verify the password of an user
                cn.Bind("gxbidding\\yuyang", "39944&*");
                Console.WriteLine("登录成功！");

                string searchBase = "dc=gxbidding,dc=com";
                int searchScope = LdapConnection.SCOPE_SUB;
                string searchFilter = "(sAMAccountName=heyinhuan)";

                LdapSearchQueue queue = cn.Search(
                    searchBase,
                    searchScope,
                    searchFilter,
                    null,
                    false,
                    (LdapSearchQueue)null,
                    (LdapSearchConstraints)null);

                LdapMessage message;
                while ((message = queue.getResponse()) != null)
                {
                    if (message is LdapSearchResult)
                    {
                        LdapEntry entry = ((LdapSearchResult)message).Entry;

                        //获取用户信息
                        var name = entry.getAttribute("name") != null ? entry.getAttribute("name").StringValue : "";
                        var mail = entry.getAttribute("mail") != null ? entry.getAttribute("mail").StringValue : "";
                        var company = entry.getAttribute("company") != null ? entry.getAttribute("company").StringValue : "";
                        var telephoneNumber = entry.getAttribute("telephoneNumber") != null ? entry.getAttribute("telephoneNumber").StringValue : "";
                        var homePhone = entry.getAttribute("homePhone") != null ? entry.getAttribute("homePhone").StringValue : "";
                        var mobile = entry.getAttribute("mobile") != null ? entry.getAttribute("mobile").StringValue : "";
                        var title = entry.getAttribute("title") != null ? entry.getAttribute("title").StringValue : "";
                        var department = entry.getAttribute("department") != null ? entry.getAttribute("department").StringValue : "";
                        var displayName = entry.getAttribute("displayName") != null ? entry.getAttribute("displayName").StringValue : "";
                        var sAMAccountName = entry.getAttribute("sAMAccountName") != null ? entry.getAttribute("sAMAccountName").StringValue : "";
                        var ID = entry.getAttribute("objectGUID") != null ? new Guid((byte[])(Array)entry.getAttribute("objectGUID").ByteValue).ToString("N") : "";

                        Console.WriteLine("sAMAccountName : " + sAMAccountName);
                        Console.WriteLine("name : " + name);
                        Console.WriteLine("mail : " + mail);
                        Console.WriteLine("company : " + company);
                        Console.WriteLine("telephoneNumber : " + telephoneNumber);
                        Console.WriteLine("homePhone : " + homePhone);
                        Console.WriteLine("mobile : " + mobile);
                        Console.WriteLine("title : " + title);
                        Console.WriteLine("department : " + department);
                        Console.WriteLine("displayName : " + displayName);
                        Console.WriteLine("ID : " + ID);

                        List<string> ADGroups = new List<string>();
                        LdapAttribute attr = entry.getAttribute("memberOf");
                        if (attr != null)
                        {
                            foreach (string value in attr.StringValueArray)
                            {
                                var x = value.Substring(3, value.IndexOf(",") - 3);
                                if (!string.IsNullOrEmpty(x))
                                {
                                    ADGroups.Add(x);
                                }
                            }
                        }
                        Console.WriteLine("ADGroups : " + string.Join(',', ADGroups.ToArray()));

                    }
                }

                cn.Disconnect();
            }

            //*/

            Console.ReadKey();
        }
    }
}
